var articulos = {
		init:function(){
			$.getJSON("familias/listafamilias", function(listafamilia) {
  				$.getJSON("unidades/listaunidades", function(listaunidades) {
	  				setupGrid(listafamilia, listaunidades);
	 			});
 			});
			var articulo = this;
			function setupGrid(listafamilia,listaunidades){
				$('#articulos').jqGrid({
		           url:'articulos/index',
		           datatype:'json',
		           mtype:'POST',
		           colNames:['id','codigo','Descripcion','Presentacion','Unidad(es) por caja','Peso(Kg)',
		                     'Precio soles','Precio dolares','Base Soles','Base dolares','Costo margen','Descuento',
		                     'Unidad','Familia','Creado','Estado'],
		           colModel:[
		               {name:"data[Articulo][id]",hidden:true},
		               {name:"data[Articulo][codigo]",index:'Articulo.codigo',width:40,align:"right",sortable:true,editable:true,editoptions:{size:8,maxlength:5},editrules:{required:true},searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}},
		               {name:"data[Articulo][descripcion]",index:'Articulo.descripcion',width:200,sortable:true,editable:true,editoptions:{size:40, maxlength:60 }, editrules:{required:true},searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}},
		               {name:"data[Articulo][presentacion]",index:'Articulo.presentacion',width:100,sortable:true,editable:true,editoptions:{size:40,maxlength:60 },editrules:{required:true},searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}},
		               {name:"data[Articulo][unidadxcaja]",index:'Articulo.unidadxcaja',width:50,align:"rigth",hidden:true,sortable:true,editable:true,editoptions:{size:15,maxlength:12},editrules:{edithidden:true,required:true, number:true},searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}},
		               {name:"data[Articulo][peso]",index:'Articulo.peso',width:50,align:"rigth",hidden:true,sortable:true,editable:true,editrules:{edithidden:true,required:true, number:true},searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}},
		               {name:"data[Articulo][costo_soles]",index:'Articulo.costo_soles',width:50,align:"rigth",hidden:true,sortable:true,editable:true,editrules:{edithidden:true,required:true, number:true},searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}},
		               {name:"data[Articulo][costo_dolares]",index:'Articulo.costo_dolares',width:50,align:"rigth",hidden:true,sortable:true,editable:true,editrules:{edithidden:true,required:true, number:true},searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}},
		               {name:"data[Articulo][costo_base_soles]",index:'Articulo.costo_base_soles',width:50,align:"rigth",hidden:true,sortable:true,editable:true,editrules:{edithidden:true,required:true, number:true},searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}},
		               {name:"data[Articulo][costo_base_dolares]",index:'Articulo.costo_base_dolares',width:50,align:"rigth",hidden:true,sortable:true,editable:true,editrules:{edithidden:true,required:true, number:true},searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}},
		               {name:"data[Articulo][costo_margen]",index:'Articulo.costo_margen',width:50,align:"rigth",hidden:true,sortable:true,editable:true,editrules:{edithidden:true,required:true, number:true},searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}},
		               {name:"data[Articulo][costo_descuento]",index:'Articulo.costo_descuento',width:50,align:"rigth",hidden:true,sortable:true,editable:true,editrules:{edithidden:true,required:true, number:true},searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}},
		               {name:"data[Articulo][unidad_id]",index:'Unidad.descripcion',width:50,align:'left',sortable:false,editable:true,edittype:"select", editrules:{required:true}, editoptions:{value:listaunidades},searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}},
					   {name:"data[Articulo][familia_id]",index:'Familia.descripcion',width:50,sortable:false,editable:true,edittype:"select", editrules:{required:true},editoptions:{value:listafamilia},searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}},
					   {name:"data[Articulo][created]",index:'Articulo.created',width:60,sortable:true,editable:false,searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}},
		               {name:"data[Articulo][estado]",index:'Estado.descripcion',width:50,sortable:false,editable:true,edittype:"select", editoptions:{value:"1:Activo;2:Desactivo"},searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}}               
		           ],
		           pager:$('#articulos-pager'),
		           rowNum:10,
		           rowList:[10,20,30],
		           viewrecords:true,
		           caption:'Articulos',
		           height:'180',
		           sortname: "Articulo.descripcion",
				   sortorder: 'desc',
		           editurl: 'articulos/index'
		       });
			   
			   $('#articulos').navGrid("#articulos-pager",{edit:true,add:true,del:true},
			   	{width:420, clearAfterEdit:false,reloadAfterSubmit:true, closeOnEscape:true,afterSubmit:articulo.afterSubmit},
				{width:420, clearAfterAdd:true,reloadAfterSubmit:true, closeOnEscape:true, afterSubmit:articulo.afterSubmit},
				{reloadAfterSubmit:true, closeOnEscape:true},
				{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew'],multipleSearch:true, closeOnEscape:true});
		       $("#articulos").jqGrid('filterToolbar',{stringResult: true,searchOnEnter : false});
		       jQuery("#articulos").jqGrid('setGridWidth',$('#RightPane').width()-50);
			}
		},
		afterSubmit: function(response){ var respuesta = JSON.parse(response.responseText); return [respuesta.respuesta,respuesta.mensaje];}
};